from django.shortcuts import render,redirect
from django.views import View
from django.http import JsonResponse
from .cartmanager import getCartManger

# Create your views here.
class CartView(View):
    def post(self,request):
        request.session.modifed = True

        type = request.POST.get('type')
        if type == 'add' :
            self.add(request)
            return redirect('cart.html')
        elif type=='delete':
            self.delete(request)
            return JsonResponse({'result':True})
        elif type=='plus':
            self.plus(request)
            return JsonResponse({'result':True})
        elif type=='minus':
            self.minus(request)
            return JsonResponse({'result':True})

    def add(self, request):
        cart_manager = getCartManger(request)
        cart_manager.add(**request.POST.dict())


    def delete(self,request):
        cart_manager = getCartManger(request)
        cart_manager.delete(**request.POST.dict())


    def plus(self,request):
        cart_manager = getCartManger(request)
        cart_manager.update(step=1,**request.POST.dict())


    def minus(self,request):
        cart_manager = getCartManger(request)
        cart_manager.update(step=-1,**request.POST.dict())
class CartListView(View):
    def get(self,request):
        cart_manager = getCartManger(request)
        cart_items = cart_manager.queryAll()
        print(cart_items)
        return  render(request,'cart.html',{'cartItems':cart_items})


















